package dao.daoimpl;

import dao.*;
import model.*;
import util.DBTool;
import java.sql.*;

/**
 * Created by 杨珂 on 2016/8/9.
 * 用户表Dao实现
 */
public class UserDaoImpl implements UserDao {

	@Override
	public int insertUser(User user) {
		//若姓名重复，则不插入，返回-1
		if(this.selectUserByName(user.getUserName())!=null)
			return -1;
		//否则插入
		int nextId = 0;
		Connection conn = null;
		PreparedStatement prst = null;
		String insertsql = "insert into user (USER_ID,USER_NAME,USER_ADDRESS,USER_CAREER,OPEN_TIME) values(?,?,?,?,?)";
		String getIdsql = "select max(USER_ID) from iyoga.user; ";
		conn = DBTool.getConnection();
		PreparedStatement getNextUserId = null;
		try {
			prst = conn.prepareStatement(insertsql);
			getNextUserId = conn.prepareStatement(getIdsql);
			ResultSet rs = getNextUserId.executeQuery();

			if (rs.next()) {
				nextId = rs.getInt("max(USER_ID)") + 1;
			}
			//1234分别对应sql语句中的问号位置
			prst.setInt(1, nextId);
			prst.setString(2, user.getUserName());
			prst.setString(3, user.getUserAddress());
			prst.setString(4, user.getUserCareer());
			prst.setDate(5, new java.sql.Date(user.getOpenTime().getTime()));
			prst.executeUpdate();

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			DBTool.close(prst, conn);
		}
		return nextId;
	}

	@Override
	//通过姓名查找User，找不到返回null
	public User selectUserByName(String userName) {

		Connection conn = null;
		PreparedStatement prst = null;
		String selectByIdsql = "select * from iyoga.user where user_name=?; ";
		conn = DBTool.getConnection();
		try {
			prst = conn.prepareStatement(selectByIdsql);
			prst.setString(1, userName);
			ResultSet rs = prst.executeQuery();
			if (rs.next()) {
				User user = new User();
				user.setUserId(rs.getInt("user_id"));
				user.setUserName(rs.getString("user_name"));
				user.setUserCareer(rs.getString("user_career"));
				user.setUserAddress(rs.getString("user_address"));
				user.setOpenTime(new java.util.Date(rs.getDate("open_time").getTime()));
				return user;

			}

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			DBTool.close(prst, conn);
		}
		return null;
	}

	@Override
	//通过Id查到User，找不到返回null
	public User selectUserById(int userId) {

		Connection conn = null;
		PreparedStatement prst = null;
		String selectByIdsql = "select * from iyoga.user where user_id=?; ";
		conn = DBTool.getConnection();
		try {
			prst = conn.prepareStatement(selectByIdsql);
			prst.setInt(1, userId);
			ResultSet rs = prst.executeQuery();
			if (rs.next()) {
				User user = new User();
				user.setUserId(rs.getInt("user_id"));
				user.setUserName(rs.getString("user_name"));
				user.setUserCareer(rs.getString("user_career"));
				user.setUserAddress(rs.getString("user_address"));
				user.setOpenTime(new java.util.Date(rs.getDate("open_time").getTime()));
				return user;
			}

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			DBTool.close(prst, conn);
		}
		return null;
	}

}
